<template>
  <el-card>
    <div class="header-box">
      <!-- 关键词-->
      <div>
        <el-input
          v-model="queryInfo.keywords"
          clearable
          placeholder="请输入搜索关键字"
          @input="handleResetPage"
        >
        </el-input>
      </div>
      <div>
        <el-select
          v-model="queryInfo.status"
          clearable
          placeholder="请选择状态"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.status"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" @click="handleResetPage">
          搜索
          <i class="el-icon-search el-icon--right"></i>
        </el-button>
      </div>
    </div>

    <div class="flex-content">
      <el-table
        v-loading="isLoading.table"
        :data="dataList"
        style="width: 100%"
      >
        <el-table-column align="center" label="订单号" prop="order_sn">
        </el-table-column>

        <el-table-column align="center" label="退款金额" prop="refund_cash">
        </el-table-column>

        <el-table-column align="center" label="退款现金" prop="refund_balance">
        </el-table-column>

        <el-table-column align="center" label="退款清单">
          <template v-slot="{ row }">
            <el-button
              size="mini"
              type="primary"
              @click="handleRefundItem(row)"
            >
              {{ row.refundItem.length }}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column align="center" label="退款记录">
          <template v-slot="{ row }">
            <el-button
              size="mini"
              type="primary"
              @click="handleRefundRecord(row)"
            >
              {{ row.refundRecord.length }}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column align="center" label="状态" prop="status">
          <template v-slot="{ row }">
            <el-tag type="primary"> {{ row.status.trans }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column align="center" label="申请时间" prop="apply_time">
        </el-table-column>

        <el-table-column align="center" label="审核时间" prop="check_time">
        </el-table-column>

        <el-table-column align="center" label="备注" prop="remark">
        </el-table-column>

        <el-table-column align="center" label="操作" width="225">
          <template v-slot="{ row }">
            <el-button
              v-if="row.status.origin === 0"
              size="mini"
              type="primary"
              @click="handleAdopt(row)"
            >
              通过
            </el-button>
            <el-button
              v-if="row.status.origin === 0"
              size="mini"
              type="danger"
              @click="handleRefuse(row)"
            >
              拒绝
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页-->
    <div class="footer-box">
      <pagination
        :limit.sync="queryInfo.rows"
        :page.sync="queryInfo.page"
        :total="totalCount"
        @pagination="getData"
      >
      </pagination>
    </div>

    <!-- 详情-->
    <Record
      v-if="isDialog.record"
      :info="dataInfo"
      @close="closeDialog"
    ></Record>
    <!-- 详情-->
    <Item v-if="isDialog.item" :info="dataInfo" @close="closeDialog"></Item>
  </el-card>
</template>

<script>
import { adopt, lists as getList, refuse } from "@/api/orderRefund";
import Record from "./components/record.vue";
import Item from "./components/item.vue";

export default {
  name: "Order",
  components: {
    Record,
    Item,
  },
  data() {
    return {
      // 查询条件
      queryInfo: {
        page: 1,
        rows: 10,
        keywords: "",
        status: 0,
      },
      // 总页数
      totalCount: 0,
      // 列表
      dataList: [],
      // 详情
      dataInfo: {},
      // 筛选项
      filterItem: {
        status: [],
      },
      // 加载
      isLoading: {
        table: false,
      },
      // 弹窗
      isDialog: {
        record: false,
        item: false,
      },
      // 防抖
      queryId: +new Date(),
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 列表
    getData() {
      this.isLoading.table = true;
      this.queryId++;
      let queryId = this.queryId;
      getList(this.queryInfo)
        .then((res) => {
          if (queryId !== this.queryId) {
            return;
          }
          this.dataList = res.data.list;
          this.totalCount = res.data.total;
          this.filterItem.status = res.data.status_screen;
        })
        .finally(() => {
          this.isLoading.table = false;
        });
    },
    // 同意
    handleAdopt(row) {
      this.$confirm(" 此操作将通过审核，是否确定?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== "confirm") {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
            close();
            return false;
          } else {
            ctx.confirmButtonLoading = true;
            adopt({
              refund_id: row.refund_id,
            })
              .then(() => {
                this.getData();
                close();
              })
              .finally(() => {
                ctx.confirmButtonLoading = false;
              });
          }
        },
      });
    },
    // 拒绝
    handleRefuse(row) {
      this.$confirm(" 此操作将通过审核，是否确定?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== "confirm") {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
            close();
            return false;
          } else {
            ctx.confirmButtonLoading = true;
            refuse({
              refund_id: row.refund_id,
            })
              .then(() => {
                this.getData();
                close();
              })
              .finally(() => {
                ctx.confirmButtonLoading = false;
              });
          }
        },
      });
    },
    // 详情
    handleRefundItem(row) {
      this.dataInfo = row;
      this.isDialog.item = true;
    },
    // 详情
    handleRefundRecord(row) {
      this.dataInfo = row;
      this.isDialog.record = true;
    },
    // 重置页面数据
    handleResetPage() {
      this.queryInfo.page = 1;
      this.getData();
    },
    //关闭
    closeDialog(val) {
      if (!val) {
        this.getData();
      }
      this.isDialog.record = false;
      this.isDialog.item = false;
    },
  },
};
</script>

<style lang="scss" scoped></style>
